package com.qxzn.module.mine.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MoreSettingsActivity extends BaseActivity {

    @Override
    public void onEventBus(EventBusBean busBean) {
        super.onEventBus(busBean);
        if (busBean.getId() == Constant.EventBusId.SWITCH_DEVICE_ID) {
            updateRobot();
        }
    }

    private void updateRobot() {
        ((TextView)findViewById(R.id.device_name)).setText(SharePreferenceUtil.getDeviceName(this));
        ((TextView)findViewById(R.id.device_code)).setText(SharePreferenceUtil.getDeviceId(this));
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_more_settings;
    }

    @Override
    protected void initView() {
        updateRobot();
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(R.string.more_robot_settings_title);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }


    @OnClick({R2.id.device_name_btn, R2.id.night_mode_btn, R2.id.unbind_btn})
    public void onViewClicked(View view) {
        if (CommonUtil.isFastClick()) {
            return;
        }
        int i = view.getId();
        if (i == R.id.device_name_btn) {
            // 设备名称
            startActivity(new Intent(this, ModifyNameActivity.class)
                    .putExtra(ModifyNameActivity.EXTRA_TYPE, ModifyNameActivity.TYPE_ROBOT));
        } else if (i == R.id.night_mode_btn) {
            // 夜间模式
            startActivity(new Intent(this, RobotNightModeActivity.class));
        } else if (i == R.id.unbind_btn) {
            showDialog("解除绑定", "解除绑定后，您将从家庭圈退出，无法再访问该设备。", "暂不", "确认解绑", null, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    unbindRobot();
                }
            });
//            showDialog("解除绑定", "解除绑定后，家庭圈将被解散，家庭成员将无法访问该设备，同时该设备将退出奇信智能账号的登录。", "暂不", "确认解绑", null, new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    unbindRobot();
//                }
//            });
        }
    }

    private void unbindRobot() {
        JSONObject params = new JSONObject();
        params.put("device_id", SharePreferenceUtil.getDeviceId(this));
        params.put("username", SharePreferenceUtil.getUserName(this));
        HttpRequest.getService().unbindRobot(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                hideProgress();
                BaseBean bean = response.body();
                if (bean.getCode() == 200) {
                    ToastUtil.showTextToast(getApplicationContext(),"您已退出该家庭圈");
                    SharePreferenceUtil.setDeviceName(getApplicationContext(),"");
                    SharePreferenceUtil.setDeviceId(getApplicationContext(),"");
                    EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.SWITCH_DEVICE_ID).build());
                    EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.GET_DEVICE).build());
                    finish();
                } else {
                    ToastUtil.showTextToast(MoreSettingsActivity.this, bean.getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                hideProgress();
                ToastUtil.showTextToast(MoreSettingsActivity.this, getString(R.string.network_error));
            }
        });
    }

}
